package com.crm.jpaandthymeleaf;


import com.crm.jpaandthymeleaf.entity.SysUser;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.*;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author 周敬
 * @version 1.0
 * @packageName com.crm.jpaandthymeleaf
 * @fileName RedisTemplateTester
 * @createTime 2021/12/20-15:07-星期一
 * @lastModify 2021/12/20-15:07-星期一
 */
@SpringBootTest
public class RedisTemplateTester {
    //操作字符串数据
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    //操作其他数据类型
    @Resource
    private RedisTemplate redisTemplate;

    public void test(){
        //向redis里存入数据和设置缓存时间
        stringRedisTemplate.opsForValue().set("test","100",60*10, TimeUnit.SECONDS);
        //val做-1的操作
        stringRedisTemplate.boundValueOps("test").increment(-1);
        //根据key获取缓存中的val
        stringRedisTemplate.opsForValue().get("test");
        //val+1
        stringRedisTemplate.boundValueOps("test").increment(1);
        //根据key获取过期时间
        stringRedisTemplate.getExpire("test");
        //根据key获取过期时间并换算成指定单位
        stringRedisTemplate.getExpire("test",TimeUnit.SECONDS);
        //根据key删除缓存
        stringRedisTemplate.delete("test");
        //检查key是否存在，返回boolean值
        stringRedisTemplate.hasKey("44884");
        //向指定key中存放set集合
        stringRedisTemplate.opsForSet().add("red_123","1","2","3");
        //设置过期时间
        stringRedisTemplate.expire("red_123",1000,TimeUnit.MICROSECONDS);
        //根据key查看集合中是否存在指定数据
        stringRedisTemplate.opsForSet().isMember("red_123",1);
        //根据key获取set集合
        stringRedisTemplate.opsForSet().members("red_123");
    }

    @Test
    public void testString(){
        stringRedisTemplate.opsForValue().set("name","czkt");
        boolean equals = "czkt".equals(stringRedisTemplate.opsForValue().get("name"));
        System.out.println("值为："+equals);
    }

    @Test
    public void testObj(){
        SysUser user = new SysUser("czkt","123456",1L,null);
        ValueOperations<String,SysUser> operations = redisTemplate.opsForValue();
        operations.set("zhoujing.crm.user",user);
        SysUser user1 = operations.get("zhoujing.crm.user");
        System.out.println("user.userName:"+user1.getUsrName());
    }

    @Test
    public void testExpire() throws InterruptedException {
        SysUser user = new SysUser("czkt","123456",1L,null);
        ValueOperations<String,SysUser> operations = redisTemplate.opsForValue();
        operations.set("expire",user);
//        Thread.sleep(150);
        Boolean exists = redisTemplate.hasKey("expire");
        if(exists){
            System.out.println("exists is true");
        }else{
            System.out.println("exists is false");
        }
    }

    @Test
    public void testHash(){
        HashOperations<String,Object,Object> hash = redisTemplate.opsForHash();
        hash.put("hash","name","zhoujing");
        String value = (String)hash.get("hash","name");
        System.out.println("hash name'name:"+value);
    }

    @Test
    public void testList(){
        ListOperations<String,String> list = redisTemplate.opsForList();
        list.leftPush("list","accp");
        list.leftPush("list","bdqn");
        list.leftPush("list","czkt");
        String value = list.leftPop("list");
        String rightPop = list.rightPop("list");
        String index = list.index("list", 2);
        System.out.println(value);
        System.out.println(rightPop);
        System.out.println(index);
    }

}
